﻿using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProjectDocController : ControllerBase
    {

        private readonly IProjectDocService _docService;
        private readonly IAuditLogService _auditLogService;

        public ProjectDocController(IProjectDocService projectDocService, IAuditLogService auditLogService)
        {
            _docService = projectDocService;
            _auditLogService = auditLogService;
        }


        [HttpPost("getDocList")]
        public async Task<IActionResult> GetDocList([FromBody] ProjectDocListParams listParams)
        {
            var res = await _docService.GetDocListAsync(listParams);
            return Ok(res);
        }

        [HttpPost("getFiles")]
        public async Task<IActionResult> GetFiles([FromBody]TaskFileListParams listParams)
        {
            var res = await _docService.GetFiles(listParams);
            return Ok(res);
        }

        [HttpPost("getDocInfo")]
        public async Task<IActionResult> GetDocInfo([FromBody] PrimaryKey key)
        {
            var res = await _docService.GetInfoById(key.Id);
            return Ok(res);
        }

        [HttpPost("getTips")]
        public async Task<IActionResult> GetTips([FromBody] PrimaryKey key)
        {
            var res = await _docService.GetTips(key.Id);
            return Ok(res);
        }

        [HttpPost("addDoc")]
        public async Task<IActionResult> AddDoc([FromBody]CreateProjectDocDto docDto)
        {
            try
            {
                await _docService.CreateAsync(docDto);
                
                // 记录文档添加日志
                await _auditLogService.LogSuccessAsync("添加", "项目文档", $"添加项目文档 {docDto.Title}", null, docDto.Title);
                
                return Ok("添加成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("添加", "项目文档", $"添加项目文档 {docDto.Title} 失败", ex.Message, null, docDto.Title);
                return Ok($"添加失败: {ex.Message}");
            }
        }

        [HttpPost("updateDoc")]
        public async Task<IActionResult> UpdateDoc([FromBody]UpdateProjectDocDto docDto)
        {
            try
            {
                await _docService.UpdateAsync(docDto.Id, docDto);
                
                // 记录文档更新日志
                await _auditLogService.LogSuccessAsync("修改", "项目文档", $"修改项目文档 {docDto.Title}", docDto.Id, docDto.Title);
                
                return Ok("更新成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("修改", "项目文档", $"修改项目文档 {docDto.Title} 失败", ex.Message, docDto.Id, docDto.Title);
                return Ok($"更新失败: {ex.Message}");
            }
        }

        [HttpPost("delDoc")]
        public async Task<IActionResult> DelDoc([FromBody]PrimaryKey key)
        {
            try
            {
                // 先获取文档信息用于日志记录
                var doc = await _docService.GetInfoById(key.Id);
                var data = (ProjectDocDto)doc.Data;
                var docTitle = data.Title ?? "未知文档";
                
                await _docService.DeleteAsync(key.Id);
                
                // 记录文档删除日志
                await _auditLogService.LogSuccessAsync("删除", "项目文档", $"删除项目文档 {docTitle}", key.Id, docTitle);
                
                return Ok("删除成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("删除", "项目文档", "删除项目文档失败", ex.Message, key.Id);
                return Ok($"删除失败: {ex.Message}");
            }
        }

    }
}
